package com.oocl.producer;

import java.util.ArrayList;
import java.util.List;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageProducer;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.oocl.dao.pojo.Book;
import com.oocl.file2bookdata.FileToBookData;

public class BookData2JSonWithThread implements Runnable {

	@Override
	public void run() {
		List<Book> bList=new ArrayList<Book>();
		FileToBookData ftd=new FileToBookData();
		try {
			bList=ftd.getBookDataFromFile("book.data");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ObjectMapper mapper=new ObjectMapper();
		for (int i = 0; i < bList.size(); i++) {
			String s;
			try {
				s = mapper.writeValueAsString(bList.get(i));
//				jsonProducer(s);
				Connection con=null;
				ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://127.0.0.1:61616");
				Destination queue=new ActiveMQQueue("Lionel");
				try {
					con=factory.createConnection();
					con.start();
					Session sen=con.createSession(false, Session.AUTO_ACKNOWLEDGE);
					MessageProducer mp=sen.createProducer(queue);
					TextMessage msg=sen.createTextMessage(s);
					mp.send(msg);
					mp.close();
					sen.close();
					con.close();
				} catch (JMSException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			} catch (JsonProcessingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		
	}

}
